import VueRouter from "vue-router";
import Login from '../pages/Login'
import Index from '../pages/Index/Index.vue'
import IndexMain from '../pages/Index/IndexMain.vue'
import userManagement from '../pages/userManagement'
import agentManagement from '../pages/agentManagement'
import projectManagement from '../pages/projectManagement'
import healthManagement from '../pages/healthManagement'

// 创建路由器
let router = new VueRouter({
  routes: [
    {
      path: "/",
      redirect: "/login",
    },
    {
      path: '/login',//登录页
      component: Login
    },
    {
      path: '/index',
      component: Index,
      redirect:'/index/indexMain',
      children:[
        {
          path:'indexMain',//首页
          name:'home',
          component:() => import('../pages/Administrators/index.vue')
        },
        {
          path:'userManagement',//用户管理
          name:'userManagement',
          component:userManagement,
          redirect:'/index/userManagement/user',
          children:[
            {
              path:'user',
              name:'user',
              component: ()=>import('../pages/userManagement/index.vue')
            }
          ]
        },
        {
          path:'agentManagement',//部门管理
          name:'agentManagement',
          component:agentManagement,
          redirect:'/index/agentManagement/agent',
          children:[
            {
              path:'agent',
              name:'agent',
              component: () => import('../pages/agentManagement/index.vue')
            }
          ]
        },
        {
          path:'projectManagement',//项目管理
          name:'projectManagement',
          component:projectManagement,
          redirect:'/index/projectManagement/project',
          children:[
            {
              path:'project',
              name:'project',
              component:()=>import('../pages/projectManagement/index.vue')
            }
          ]
        },
        {
          path:'attendanceManagement',//出勤管理
          name:'attendanceManagement',
          component:()=>import('../pages/attendanceManagement/index.vue'),
          redirect:"/index/attendanceManagement/participation",
          children: [
            {
              path: 'participation',//出勤
              name: 'participation',
              component:()=>import('../pages/attendanceManagement/Participation.vue')
            },
            {
              path:'takeOff',//请假
              name:'takeOff',
              component:()=>import('../pages/attendanceManagement/takeOff.vue')
            }
          ]
        },
        {
          path:'healthManagement',//健康管理
          name:'healthManagement',
          component:healthManagement,
          redirect:'/index/healthManagement/health',
          children:[
            {
              path:'health',
              name:'health',
              component:()=>import('../pages/healthManagement/healthList.vue')
            }
          ]

        }
      ]
    },
    {
      path: '/admin',
      component: ()=>import('../pages/IndexMax/index.vue'),
      redirect: '/admin/adminMain',
      children: [
        {
          path: 'adminMain',
          name: 'adminHome',
          component:()=>import('../pages/Administrators/index.vue'),
        },
        {
          path: 'systemSetting',
          name: 'systemSetting',
          component: ()=>import('../pages/Administrators/SettingPage.vue'),
          redirect: '/admin/systemSetting/system',
          children: [
            {
              path: 'system',
              name: 'system',
              component: ()=>import('../pages/Administrators/adminList.vue' ),
            },
            {
              path: '/editAdmin/:id',
              name: 'editAdmin',
              component: ()=>import('../pages/Administrators/editAdmin.vue' ),
            },
            {
              path: 'createAdmin',
              name: 'createAdmin',
              component: ()=>import('../pages/Administrators/createAdmin.vue' ),
            }
          ]
        }
      ]
    }
  ]
})
router.beforeEach((to, from, next) => {
  if (to.path === '/login') {
    next()
  } else {
    let token = localStorage.getItem('info')
    if (token === null || token === '') {
      next('/login')
      console.log('token为空')
    }
    token = JSON.parse(token)
    console.log(to.path)
    if (to.path === '/index/indexMain') {
      if(token.role === 'admin') {
        next()
      }else{
        next('/login')
      }
    }
   else if(to.path === '/admin/adminMain') {
        if(token.role === 'superadmin') {
            next()
        }
        else{next('/login')}
    }
   else{
        next()
    }
  }
})
export default router
// router guide
